import kk from 'kokos'

cc.Class({
    extends: cc.Component,

    properties: {

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        const scene = cc.director.getScene()
        const canvas = scene.getChildByName('Canvas')

        this.viewLayer = canvas.getChildByName('uiLayer').getChildByName('viewLayer')
        this.goldLabel = this.viewLayer.getChildByName('goldView').getChildByName('label').getComponent(cc.Label)
        this.initEventListener()
        this.refreshGoldView()

    },

    start() {

    },

    initEventListener() {
        const eventManager = kk.event
        const eventEnum = kk.constant.EVENT
        eventManager.on(eventEnum.refreshGold, this.refreshGoldView.bind(this))

    },

    removeEventListener() {
        const eventManager = kk.event
        const eventEnum = kk.constant.EVENT
        eventManager.remove(eventEnum.refreshGold)

    },

    refreshGoldView() {
        const gold = kk.baseData.getGold()
        this.goldLabel.string = gold
    },

    onDestroy() {
        this.removeEventListener()
    }

    // update (dt) {},
});
